# -*- coding: utf-8 -*-
"""
f1score 兼顾了分类模型的准确率和召回率
Created on Tue Apr 24 09:49:35 2018

@author: Allen
"""

import numpy as np
def f1_score( precision, recall ):
    try:
        return 2 * precision * recall / ( precision + recall )
    except:
        return 0.0
    
print( f1_score( 0.5, 0.5 ) ) # 0.5
print( f1_score( 0.1, 0.9 ) ) # 0.18
print( f1_score( 0.0, 1 ) ) # 0.0
'''
可以看出，当其中一个很小的时候，整体很小。
只有两个值都很大的时候，整体才大
'''

from sklearn import datasets

digits = datasets.load_digits()
X = digits.data
y = digits.target.copy()

y[digits.target == 9] = 1
y[digits.target != 9] = 0

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, random_state = 666 )

from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit( X_train, y_train )
print( log_reg.score( X_test, y_test ) ) # 0.975555555556

y_predict = log_reg.predict( X_test )


# sklearn 中的混淆矩阵，精准率和召回率
from sklearn.metrics import confusion_matrix
confusion_matrics = confusion_matrix( y_test, y_predict )
print( confusion_matrics )
'''
[[403   2]
 [  9  36]]
'''

# 精准率
from sklearn.metrics import precision_score
print( precision_score( y_test, y_predict ) ) # 0.947368421053

# 召回率
from sklearn.metrics import recall_score
print( recall_score( y_test, y_predict ) ) # 0.8

# f1_score
from sklearn.metrics import f1_score
print( f1_score( y_test, y_predict ) ) # 0.867469879518
'''
对于该算法来说，0.8674 可以更好的反映该算法水平是怎样的，
这要比score可靠性要高。
'''